package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.YyProject;
import com.ruoyi.gcgl.mapper.YyProjectMapper;
import com.ruoyi.gcgl.service.IYyProjectService;

/**
 * 项目立项Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-17
 */
@Service
public class YyProjectServiceImpl implements IYyProjectService {
    @Autowired
    private YyProjectMapper gcProjectMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询项目立项
     * 
     * @param id 项目立项主键
     * @return 项目立项
     */
    @Override
    public YyProject selectGcProjectById(Long id){
        return gcProjectMapper.selectGcProjectById(id);
    }

    /**
     * 查询项目立项列表
     * 
     * @param gcProject 项目立项
     * @return 项目立项
     */
    @Override
    public List<YyProject> selectGcProjectList(YyProject gcProject){
        return gcProjectMapper.selectGcProjectList(gcProject);
    }

    /**
     * 新增项目立项
     * 
     * @param gcProject 项目立项
     * @return 结果
     */
    @Override
    public int insertGcProject(YyProject gcProject){
    	
    	int rows = gcProjectMapper.insertGcProject(gcProject);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcProject.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcProject.getOwner());
        variables.put("level1", gcProject.getSwyy());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("yyxiangmulixiang", String.valueOf(gcProject.getId()), variables);
    	
    	YyProject obj = new YyProject();
    	obj.setId(gcProject.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcProjectMapper.updateGcProject(obj);
    	
        return rows;
    }

    /**
     * 修改项目立项
     * 
     * @param gcProject 项目立项
     * @return 结果
     */
    @Override
    public int updateGcProject(YyProject gcProject){
    	
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcProject.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcProject.getOwner());
//        variables.put("level1", gcProject.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjxiangmulixiang", String.valueOf(gcProject.getId()), variables);
//        gcProject.setProcessInstanceId(pi.getProcessInstanceId());
    	
        return gcProjectMapper.updateGcProject(gcProject);
    }

    /**
     * 批量删除项目立项
     * 
     * @param ids 需要删除的项目立项主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectByIds(String ids){
        return gcProjectMapper.deleteGcProjectByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目立项信息
     * 
     * @param id 项目立项主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectById(Long id){
        return gcProjectMapper.deleteGcProjectById(id);
    }

	@Override
	public List<YyProject> selectGcProject4AtList(YyProject gcProject) {
		return gcProjectMapper.selectGcProject4AtList(gcProject);
	}

	@Override
	public int updateGcProject4check(YyProject gcProject) {
		return gcProjectMapper.updateGcProject(gcProject);
	}

	@Override
	public List<Map> selectZc4po(Long id) {
		return gcProjectMapper.selectZc4po(id);
	}
}